<template>
	<view class="container">
		<view class="con-box" style="margin-bottom: 50rpx;">
			<u-form labelWidth="80" labelPosition="rgith" :model="model1" :rules="rules" ref="form1">
				<u-form-item required borderBottom prop="userInfo.name1" label="封面" ref="name1">
					<view class="u-m-t-20 u-flex-1" style="flex: 1;margin-top: 20rpx;">
						<u-upload :showProgress="false" @on-uploaded="uploadSuccessNews"
							:action="`https://job.ydylmold.cn/base/upload/file`" :maxSize="50"
							:formData="{fileCode:108,fileType:100,fileParaName:'file'}" width="180rpx" height="180rpx"
							:fileList="fileList" maxCount="1">
						</u-upload>
					</view>
				</u-form-item>
				<u-form-item required label="标题" prop="userInfo.name2" borderBottom ref="name2">
					<view class="u-m-t-20 u-flex-1" style="flex: 1;margin-top: 20rpx;">
						<u-input placeholder="请填写" v-model="model1.userInfo.name2" border="none"></u-input>
					</view>
				</u-form-item>
				<u-form-item required @click="toUrl('/pages/newsPart/manaPage/parsePage')" label="正文"
					prop="userInfo.name5" borderBottom ref="name5">
					<view class="u-m-t-20 u-flex-1" style="flex: 1;margin-top: 20rpx;">
						<u-input placeholder="请完善" disabled disabledColor="#fff" v-model="model1.userInfo.name5"
							border="none"></u-input>
					</view>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
				<view v-if="!newsCompany">
					<picker mode="date" @change="changeDate" :start="$u.timeFormat(new Date(Date.now()), 'yyyy-mm-dd')">
						<u-form-item required label="有效至期" prop="userInfo.name3" borderBottom ref="name3">
							<view class="u-m-t-20 u-flex-1" style="flex: 1;margin-top: 20rpx;">
								<u-input disabled disabledColor="#fff" placeholder="请选择" v-model="model1.userInfo.name3"
									border="none"></u-input>
							</view>
							<u-icon slot="right" name="arrow-right"></u-icon>
						</u-form-item>
					</picker>
					<picker @change="changeType" :range="tabList" range-key="typeName">
						<u-form-item required label="类别" prop="userInfo.name4" borderBottom ref="name4">
							<view class="u-m-t-20 u-flex-1" style="flex: 1;margin-top: 20rpx;">
								<u-input disabled disabledColor="#fff" style="text-align: right;" placeholder="请选择"
									v-model="model1.userInfo.name4" border="none"></u-input>
							</view>
							<u-icon slot="right" name="arrow-right"></u-icon>
						</u-form-item>
					</picker>
				</view>
			</u-form>
		</view>
		<my-auth-modal></my-auth-modal>
		<button style="width: 702rpx;margin-bottom: 20rpx;" type="primary" class="u-m-y-20"
			@click="$u.throttle(submit, 1000)">{{isEdit?'修改':'发布'}}</button>
	</view>
</template>
<script>
	import {
		getToken
	} from '@/utils/token.js'
	import {
		uploadFilePromise
	} from "@/utils/upload.js"
	import {
		mapGetters
	} from 'vuex';
	import {
		insertDataComp,
		insertDataNews,
		queryPortalNewsById2,
		queryNewsById,
		queryClassType2
	} from '@/common/api/news.js'
	export default {
		data() {
			return {
				model1: {
					userInfo: {
						name1: '',
						name2: '',
						name3: '',
						name4: '',
						name5: '',
					},
				},
				fileList: [],
				tabList: [],
				rules: {
					'userInfo.name1': {
						type: 'string',
						required: true,
						message: '请上传图片',
						trigger: ['blur', 'change']
					},
					'userInfo.name2': {
						type: 'string',
						required: true,
						message: '请填写标题',
						trigger: ['blur', 'change']
					},
					'userInfo.name3': {
						type: 'string',
						required: true,
						message: '请选择有效至期',
						trigger: ['blur', 'change']
					},
					// 'userInfo.name4': {
					// 	type: 'string',
					// 	required: true,
					// 	message: '请选择类别',
					// 	trigger: ['blur', 'change']
					// },
					'userInfo.name5': {
						type: 'string',
						required: true,
						message: '请填写正文',
						trigger: ['blur', 'change']
					},
				},
				classTypeId: '',
				errorType: ['message'],
				isEdit: false,
				acItem: {},
				fileListVideo: [],
				newsCompany: '',
				filePath: 'https://www.ydylmold.cn/file/getPlayResource/'
			};
		},
		computed: {
			...mapGetters(['editHtml']),
		},
		onLoad: function(option) {
			this.newsCompany = getToken('newsCompany')
			if (option.itemId) {
				this.isEdit = true
				this.getDetailById(option.itemId)
			}
			this.queryClassType2()
		},
		onShow: function() {
			this.model1.userInfo.name5 = this.editHtml && this.editHtml.text
		},
		beforeDestroy: function() {
			this.$store.commit('setEditHtml', {
				html: '',
				text: ''
			})
		},
		methods: {
			//新闻图片上传成功
			uploadSuccessNews(e) {
				//console.log(e, 888, this.model1.userInfo.name1)
				this.model1.userInfo.name1 = e[0].response.data
			},
			toUrl(path) {
				uni.navigateTo({
					url: path
				})
			},
			changeDate(e) {
				this.model1.userInfo.name3 = e.target.value
			},
			getDetailById(id) {
				if (!id) {
					return;
				}
				let obj = {}
				let apiName = ''
				if (getToken('newsCompany')) {
					obj['companyNo'] = getToken('newsCompany')
					obj['newsId'] = id
					queryNewsById(obj).then(res => {
						if (res.data && res.data.code) {
							//console.log(9999, res)
						}
					})
				} else {
					getToken('version') == 1 ? obj['userNo'] = getToken('useNum') : obj['companyNo'] = getToken('useNum')
					obj['newsId'] = id
					queryPortalNewsById2(obj).then(res => {
						//console.log(8888, res)
						this.acItem = res.data
						obj = {
							name1: this.acItem.imgTitle || '',
							name2: this.acItem.title || '',
							name3: this.acItem.dateTo || '',
							name4: this.acItem.typeName || '',
							name5: this.acItem.content || '',
						}
						this.fileList = [{
							url: this.filePath + '108/' + this.acItem.imgTitle
						}]
						this.$store.commit('setEditHtml', {
							html: obj.name5,
							text: obj.name5
						})
						this.classTypeId = this.acItem.typeId
						this.model1.userInfo = obj
					})
				}
				
				// this.$http(apiName, obj, '加载中').then(res => {
				// 	let data = {}
				// 	if (getToken('newsCompany')) {
				// 		data = res
				// 	} else {
				// 		data = res.data
				// 	}
				// 	if (data && data.title) {
				// 		this.acItem = data
				// 		//console.log(this.acItem)
				// 		let obj = {}
				// 		if (getToken('newsCompany')) {
				// 			obj = {
				// 				name1: this.acItem.imgTitle || '',
				// 				name2: this.acItem.title || '',
				// 				name5: this.acItem.comContent || '',
				// 			}
				// 			this.fileList = [{
				// 				url: this.filePath + '182/' + this.acItem.imgTitle
				// 			}]
				// 		} else {
				// 			obj = {
				// 				name1: this.acItem.images || '',
				// 				name2: this.acItem.title || '',
				// 				name3: this.acItem.dateTo || '',
				// 				name4: this.acItem.typeName || '',
				// 				name5: this.acItem.content || '',
				// 			}
				// 			this.fileList = [{
				// 				url: this.filePath + '101/' + this.acItem.images
				// 			}]
				// 			this.classTypeId = this.acItem.topTypeId || this.acItem.typeId
				// 		}
				// 		this.$store.commit('setEditHtml', {
				// 			html: obj.name5,
				// 			text: obj.name5
				// 		})
				// 		this.model1.userInfo = obj
				// 		//console.log(obj, 89898)
				// 	}
				// })
			},
			submit() {
				this.$refs.form1.validate().then(valid => {
					if (valid) {
						let currData = this.model1.userInfo
						let params = {}
						let apiName = ''
						
						if (getToken('newsCompany')) {
							apiName = 'common.insertDataComp'
							params = {
								comContent: this.editHtml.html,
								companyId: getToken('newsCompany'),
								title: currData.name2,
								imgTitle: currData.name1,
								chainTypeId: 10016,
								newsId: this.acItem && this.acItem.newsId?this.acItem.newsId:''
							}
							if (this.acItem && this.acItem.newsId) {
								params['newsId'] = this.acItem.newsId
							}
						} else {
							params = {
								newsImage: currData.name1,
								title: currData.name2,
								dateTo: currData.name3,
								typeId: this.classTypeId,
								typeName: currData.name4,
								content: this.editHtml.html,
								companyNo: getToken('useNum'),
								newsId: this.acItem && this.acItem.newsId?this.acItem.newsId:''
							}
							//console.log(params, 999)
							insertDataNews(params).then(res => {
								if (res.data && res.data.code) {
									this.$u.toast('操作成功')
									uni.navigateBack({})
									this.$store.commit('isFresh', true)
								} else {
									this.$u.toast('提交出错')
								}
							})
						}
					}
				})
			},
			// 选择类别
			changeType(e) {
				this.classTypeId = this.tabList[e.target.value].typeId
				this.model1.userInfo.name4 = this.tabList[e.target.value].typeName
			},
			queryClassType2() {
				queryClassType2().then(res => {
					//console.log(res, 888)
					if (res.data && res.data.code) {
						this.tabList = res.data.listData
					}
				})
			},
			// 上传图片成功
			uploadSuccess(e) {
				let acCode = ''
				if (getToken('newsCompany')) {
					acCode = 182
				} else {
					acCode = 101
				}
				uploadFile(e.file, acCode).then(res => {
					if (res) {
						this.model1.userInfo.name1 = res
						this.fileList = [{
							url: e.file.url
						}]
					}
				})
			},
			// 移除图片
			uploadRemove(index) {
				this.fileList = []
			},
		},
	};
</script>
<style lang="scss" scoped>
	.container {
		.con-box {
			background-color: #FFFFFF;
			padding: 0rpx 30rpx 0rpx 30rpx;
		}

		background-color: #F8F8F8;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		min-height: calc(100vh - var(--window-top));
	}
</style>
